/**
 * @Copyright 2012, Founder Mobile Media Technology（Chongqing） Co., Ltd L.P. All
 *            rights reserved. The information contained herein is confidential
 *            and proprietary to Founder Mobile Media Technology（Chongqing） Co.,
 *            Ltd, and considered a trade secret as defined under civil and
 *            criminal statutes. Founder Mobile Media Technology（Chongqing） Co.,
 *            Ltd shall pursue its civil and criminal remedies in the event of
 *            unauthorized use or misappropriation of its trade secrets. Use of
 *            this information by anyone other than authorized employees of
 *            Founder Mobile Media Technology（Chongqing） Co., Ltd is granted
 *            only under a written non-disclosure agreement, expressly
 *            prescribing the scope and manner of such use.
 */
package com.g2room.driving.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * 
 * 
 * @createTime: Jun 19, 2013 10:08:01 AM
 * @author: <a href="mailto:gaoxg@FOUNDER.COM.CN">Xuguo Gao</a>
 * @version: 0.1
 * @lastVersion: 0.1
 * @updateTime:
 * @updateAuthor: <a href="mailto:gaoxg@FOUNDER.COM.CN">Xuguo Gao</a>
 * @changesSum:
 * 
 */
public class EmailUtil {
	 private String host = "smtp.163.com"; // smtp服务器
	private String from = "leader-12@163.com"; // 发件人地址
	private String to = "leader1212@sina.com"; // 收件人地址
	private String username = "leader-12"; // 用户名
	private String password = "123456gxg~"; // 密码
	
	public EmailUtil() {}
	
	EmailUtil(String from, String to, String username, String password) {
		this.from = from;
		this.to = to;
		this.username = username;
		this.password = password;
	}

	/**
	 * 獲取Properties
	 * @param host
	 * @return
	 */
	public Properties getProperties() {
		Properties props = new Properties();
		// 设置发送邮件的邮件服务器的属性（这里使用网易的smtp服务器）
		props.put("mail.smtp.host", host);
		// 需要经过授权，也就是有户名和密码的校验，这样才能通过验证
		props.put("mail.smtp.auth", "true");
		return props;
	}

	public class MyAuthenticator extends Authenticator {  
	    private String username = null;  
	    private String password = null;  
	  
	    public MyAuthenticator() {};  
	  
	    public MyAuthenticator(String username, String password) {  
	        this.username = username;  
	        this.password = password;  
	    }  
	    protected PasswordAuthentication getPasswordAuthentication() {  
	        return new PasswordAuthentication(username, password);  
	    }  
	}  

	/**
	 * 以HTML格式发送邮件
	 * 
	 * @param content 待发送的邮件内容
	 */
	public boolean sendHtmlMail(String subject, String content) {
		// 判断是否需要身份认证
		Properties pro = getProperties();
		// 密碼校驗
		Authenticator authenticator = new MyAuthenticator(username, password);
		// 根据邮件会话属性构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
		try {
			// 根据session创建一个邮件消息
			Message mailMessage = new MimeMessage(sendMailSession);
			// 创建邮件发送者地址
			Address from = new InternetAddress(this.from);
			// 设置邮件消息的发送者
			mailMessage.setFrom(from);
			// 创建邮件的接收者地址，并设置到邮件消息中
			Address to = new InternetAddress(this.to);
			// Message.RecipientType.TO属性表示接收者的类型为TO
			mailMessage.setRecipient(Message.RecipientType.TO, to);
			// 设置邮件消息的主题
			mailMessage.setSubject(subject);
			// 设置邮件消息发送的时间
			mailMessage.setSentDate(new Date());
			// MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
			Multipart mainPart = new MimeMultipart();
			// 创建一个包含HTML内容的MimeBodyPart
			BodyPart html = new MimeBodyPart();
			// 设置HTML内容
			html.setContent(content, "text/html; charset=utf-8");
			mainPart.addBodyPart(html);
			// 将MiniMultipart对象设置为邮件内容
			mailMessage.setContent(mainPart);
			// 发送邮件
			Transport.send(mailMessage);
			return true;
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}
		return false;
	}

	public static void main(String[] args) {
		EmailUtil cn = new EmailUtil();
		// 设置发件人地址、收件人地址和邮件标题
		cn.sendHtmlMail("qq", "我是发送的<br>aaa");
	}
}
